//
//  SYFunctionCollectionViewCell.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/12.
//  Copyright © 2019年 李文强. All rights reserved.
//

#import "SYFunctionCollectionViewCell.h"

@interface SYFunctionCollectionViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *headerImage;
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UILabel *dateLab;
@property (weak, nonatomic) IBOutlet UILabel *timerLab;
@property (strong, nonatomic) NSIndexPath *indexPath;

@property (assign, nonatomic) BOOL isChar;
@property (copy, nonatomic) NSString *isFrom;
@property (weak, nonatomic) IBOutlet UIButton *upBtn;

@end


@implementation SYFunctionCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector( longPress:)];
    [self addGestureRecognizer:longPress];
}
- (void)functionCollectionModel:(SYCollectionModel *)model indexPath:(NSIndexPath *)index setIsChar:(BOOL)isChar isFrom:(NSString *)isfrom{
    self.model = model;
    self.indexPath = index;
    self.upBtn.hidden = YES;
    UIImage *placeholderImage = nil;
    if ([model.type isEqualToString:@"1"] || [model.type isEqualToString:@"2"]) {//新建文件 zy_collection_mrtp
        placeholderImage = [UIImage imageNamed:@"zy_collection_xjwjj"];
    } else if ([model.type isEqualToString:@"5"]) {//微课程
        placeholderImage = [UIImage imageNamed:@"zy_view_wdvs"];
    } else if ([model.type isEqualToString:@"3"]) {
        if (self.isChar) {
            
        } else {//群聊进来不需要上传
            self.upBtn.hidden = NO;
        }
    }
    else {//默认课程
        placeholderImage = [UIImage imageNamed:@"zy_collection_mrtp"];
    }
    [self.headerImage sd_setImageWithURL:[NSURL URLWithString:model.fileIcon] placeholderImage:placeholderImage];
    self.titleLab.text = model.titleName;
    self.dateLab.text = model.dateTime;
    if ([model.type isEqualToString:@"1"]) {
        self.timerLab.text = SYStringWithPickFormat(model.timer, @"项");
    } else {
        if ([model.type isEqualToString:@"3"] || [model.type isEqualToString:@"9"]) {
//            if (model.chec) {
//                <#statements#>
//            }
            self.timerLab.text = model.checkStatusName;
        } else {
            self.timerLab.text = model.timer;
        }
        
    }
    
    _isFrom = isfrom;
    _isChar = isChar;
    
}

- (void)setStatus:(SYCollectionViewType)status {
    _status = status;
    
}
//- (void)setIsChar:(BOOL)isChar isFrom:(NSString *)isfrom {
//    _isChar = isChar;
//    _isFrom = isfrom;
//
//}


- (void)longPress:(UILongPressGestureRecognizer *)longPress {
    
    if (_isChar == YES || _isFrom) {
        return;
    }
    
    if (longPress.state == UIGestureRecognizerStateBegan) {
        
        [self becomeFirstResponder];
        
        UIMenuController *menu = [UIMenuController sharedMenuController];
        [menu setTargetRect:CGRectMake(-40, 0, 180, 20) inView:self];
        // 定义两个菜单a和b
        UIMenuItem *a = [[UIMenuItem alloc] initWithTitle:@"重命名"
                                                   action:@selector(aAction)];
        UIMenuItem *b = [[UIMenuItem alloc] initWithTitle:@"移动"
                                                   action:@selector(bAction)];
        UIMenuItem *c = [[UIMenuItem alloc] initWithTitle:@"删除"
                                                   action:@selector(cAction)];
        UIMenuItem *d = [[UIMenuItem alloc] initWithTitle:@"上传"
                                                   action:@selector(dAction)];
        UIMenuItem *f = [[UIMenuItem alloc] initWithTitle:@"分享"
                                                   action:@selector(fAction)];
        
        //我的作品，我的微课 可以 移动，删除,重命名   微课子课只能重命名，移动  我的收藏 只能移出
        if (self.status == SYCollectionViewTypeMinWenJian) {
            menu.menuItems = @[a,b,c];
        } else if (self.status == SYCollectionViewTypeWeiKeTiaoZhuan) {
            menu.menuItems = @[a,c];
        } else if (self.status == SYCollectionViewTypeShouCang) {
            menu.menuItems = @[b,c];
        } else if (self.status == SYCollectionViewTypeWeiKe) {
            if ([self.model.type isEqualToString:@"3"]) {
                menu.menuItems = @[a,d];
            }else if ([self.model.type isEqualToString:@"9"]){
                menu.menuItems = @[a,d,f];

            } else {
                menu.menuItems = @[a];
            }
            
        }
        else {
            menu.menuItems = @[];
        }
        
        
       
        
        menu.menuVisible = YES;
    }
}
- (BOOL)canBecomeFirstResponder
{
    if (self.status == SYCollectionViewTypeWeiKe || self.status == SYCollectionViewTypeMinWenJian||self.status == SYCollectionViewTypeWeiKeTiaoZhuan || self.status == SYCollectionViewTypeShouCang) {
        return YES;
    } else {
        return NO;
    }
    
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    // 显示一个系统的paste以及自定义的a，b菜单
    if (
        action == @selector(aAction) ||
        action == @selector(bAction)||
        action == @selector(cAction)||
        action == @selector(dAction)||
        action == @selector(fAction))
        
    {
        return YES;
    }
    else{
        return NO;
    }
}
//重命名2
- (void)aAction {
    [self backOffAgentIndex:2];
}
//移动3
- (void)bAction {
    [self backOffAgentIndex:3];
}
//删除1
- (void)cAction {
    [self backOffAgentIndex:1];
}
//  分享
-(void)fAction{
    
    [self backOffAgentIndex:4];

}
//上传
- (void)dAction {
    
    [SYRequestAPI zy_requestEmployeeResourceFiledId:self.model.zyid success:^(BOOL isSuccess) {
        
    } fail:^(NSError *error) {
        
    }];
}


//代理的会掉
- (void)backOffAgentIndex:(NSInteger)index {
    if ([self.delegate respondsToSelector:@selector(selectLogPressType:indexPath:)]) {
        [self.delegate selectLogPressType:index indexPath:self.indexPath];
    }
    
    
}
- (IBAction)upBtnClick:(UIButton *)sender {
    
}

@end







